package com.zhixiang.core.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.captcha.Captcha;
import nl.captcha.servlet.CaptchaServletUtil;

public class SimpleCaptchaServlet extends HttpServlet
{
  private static final long serialVersionUID = 1L;
  private static final String PARAM_HEIGHT = "height";
  private static final String PARAM_WIDTH = "width";
  protected int _width = 200;
  protected int _height = 50;
//  private static Log logger = LogFactory.getLog(StartupListener.class);

  public void init() throws ServletException {
   /* Timer timer = new Timer();
    timer.schedule(new TimerTask()
    {
      public void run() {
        Boolean is_true = AppUtil.getStart();
        if (!is_true.booleanValue()) {
          SimpleCaptchaServlet.logger.error("未检查到授权或授权已经过期....");
          SimpleCaptchaServlet.logger.error("服务停止，请尽快购买授权....");
          System.exit(0);
        } else {
          SimpleCaptchaServlet.logger.info("检查授权成功....");
        }
      }
    }
    , 30000L);*/
    if (getInitParameter(PARAM_HEIGHT) != null)
      this._height = Integer.valueOf(getInitParameter(PARAM_HEIGHT))
        .intValue();
    if (getInitParameter(PARAM_WIDTH) != null)
      this._width = Integer.valueOf(getInitParameter(PARAM_WIDTH)).intValue();
  }

  public void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException, IOException
  {
    Captcha localCaptcha = new Captcha.Builder(this._width, this._height)
      .addText().addBackground().addNoise().build();
    CaptchaServletUtil.writeImage(paramHttpServletResponse, 
      localCaptcha.getImage());
    paramHttpServletRequest.getSession().setAttribute("simpleCaptcha", 
      localCaptcha);
  }
}

